package com.yunhe.marketing.util;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/3/15 9:16
 */
public class IpAddressUtil {

    public static String getIpAddress(String ip) throws IOException {
        URL netchinaren=new URL ("http://whois.pconline.com.cn/?ip=" +ip);
        BufferedReader in = new BufferedReader(new InputStreamReader(netchinaren.openStream(),"GBK"));
        String inputline;
        while ((inputline=in.readLine())!=null) {
            if (inputline.contains("位置：")){
                String replace = inputline.replace("\t", "");
                String substring = replace.substring(3);
                String[] s = substring.split(" ");
                return s[0];
            }
        }
        in.close();
        return null;
    }

}
